<?php

namespace Futures\Controller;
use Think\Controller;
use Futures\Model\OptionModel;

/**
 *  代理商首页控制器
 */
class AgentIndexController extends AgentBaseController {

	public function index(){

		$qqlcd_Model = new OptionModel('order');						//恒指订单
		$ouyuan_model = new OptionModel('euro_order');			//欧元订单

		$gold_model = new OptionModel('goldorder');				//伦敦金订单
		$dollar_model = new OptionModel('dollarorder');				//美元订单
		$dax_model = new OptionModel('daxorder');				//徳指订单
		$bite_model = new OptionModel('biteorder');       //比特比订单
		/*我的资金*/
		$user = M('futures_user_bank');
		$uid = session('AGENT.user_id');
		$map['user_id'] = $uid;

        /*关联查询客户信息*/
        $list = $user->join('wp_follow as a ON wp_futures_user_bank.user_id = a.id')
        ->where($map)
        ->field('wp_futures_user_bank.*,a.nickname,a.money')->order('id desc')->select();

        //查询下级余额
        $map['_string'] = "(route like '{$uid}') or (route regexp ',{$uid},') or (route regexp '^{$uid},') or (route regexp ',{$uid}$')";
        $money = M('follow')->where($map)->field('id,money')->select();
        $route[] = $uid;
        foreach ($money as $v){
        	$route[] = $v['id'];
        	$sub_money = $sub_money+$v['money'];
        }

		//时间段搜索
    	$stime = strtotime($_REQUEST['start_time']);
    	$etime = strtotime($_REQUEST['end_time']);

			if($stime){
				$cms['a.create_time'] = array('egt',$stime);//佣金
				$odr['a.create_time']  = array('egt',$stime);//螺纹钢
				$rec['a.create_time'] = array('egt',$stime);//充值时间
				$cash['a.add_time'] = array('egt',$stime);	//提现时间
				$mongo_sql['start_time'] = array('$gte'=>$stime);	//下单时间
			}
			if($etime){
				$end_time = $etime+86400;
				$cms['_string'] = 'a.create_time <='.$end_time;//佣金
				$rec['_string'] = 'a.create_time <='.$end_time;//充值时间
				$cash['_string'] = 'a.add_time <='.$end_time;	//提现时间
				$odr['_string'] = 'a.create_time <='.$end_time;       //螺纹钢
				if(empty($stime)){
					$mongo_sql['start_time'] = array();
				}
				$mongo_sql['start_time'] += array('$lte'=>$end_time);	//下单时间
			}
        /*资金汇总*/
				if($etime){
					$route_sql = " and ((b.route like '{$uid}') or (b.route regexp ',{$uid},') or (b.route regexp '^{$uid},') or (b.route regexp ',{$uid}$') or b.id = {$uid})";
				}else{
					$route_sql = " (b.route like '{$uid}') or (b.route regexp ',{$uid},') or (b.route regexp '^{$uid},') or (b.route regexp ',{$uid}$') or b.id = {$uid}";
				}

				$rec['_string'] .= $route_sql;
				$cash['_string'] .= $route_sql;
				$cms['_string'] .= $route_sql;
				$odr['_string'] .= $route_sql;
				$sql['_string'] .= $route_sql;
				$mongo_sql['user_id'] = array('in',$route);

				$rec['a.is_pay'] = 1;
				$cash['a.is_pay'] = array('neq',2);
        //欧元下单数据
        $oy_order = $ouyuan_model->where($mongo_sql)->field('invest,profit')->select();
				$profit['oy_order']['title'] = '欧元订单';
        foreach($oy_order as $v){
        	$profit['oy_order']['invest'] += $v['invest'];
        	if($v['profit'] > 0){
        		$profit['oy_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['oy_order']['loss'] += $v['profit'];
        	}
        }
        $profit['oy_order']['pal'] = -$profit['oy_order']['loss']-$profit['oy_order']['profit'];//计算公司盈亏

        //恒指下单数据
        $order = $qqlcd_Model->where($mongo_sql)->field('invest,profit')->select();
				$profit['hz_order']['title'] = '恒指订单';
        foreach($order as $v){
        	$profit['hz_order']['invest'] += $v['invest'];
        	if($v['profit'] > 0){
        		$profit['hz_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['hz_order']['loss'] += $v['profit'];
        	}
        }
        $profit['hz_order']['pal'] = -$profit['hz_order']['loss']-$profit['hz_order']['profit'];//计算公司盈亏

				//伦敦金下单数据
        $gold_order = $gold_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['gold_order']['title'] = '伦敦金订单';
        foreach($gold_order as $v){
          $profit['gold_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['gold_order']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['gold_order']['loss'] += $v['profit'];
          }
        }
        $profit['gold_order']['pal'] = -$profit['gold_order']['loss']-$profit['gold_order']['profit'];//计算公司盈亏

				//美元下单数据
        $dollar_order = $dollar_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['dollarorder']['title'] = '美元订单';
        foreach($dollar_order as $v){
          $profit['dollarorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['dollarorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['dollarorder']['loss'] += $v['profit'];
          }
        }
        $profit['dollarorder']['pal'] = -$profit['dollarorder']['loss']-$profit['dollarorder']['profit'];//计算公司盈亏

				//徳指下单数据
        $dax_order = $dax_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['daxorder']['title'] = '徳指订单';
        foreach($dax_order as $v){
          $profit['daxorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['daxorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['daxorder']['loss'] += $v['profit'];
          }
        }
        $profit['daxorder']['pal'] = -$profit['daxorder']['loss']-$profit['daxorder']['profit'];//计算公司盈亏

				//比特币下单数据
        $bite_order = $bite_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['biteorder']['title'] = '比特币订单';
        foreach($bite_order as $v){
          $profit['biteorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['biteorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['biteorder']['loss'] += $v['profit'];
          }
        }
        $profit['biteorder']['pal'] = -$profit['biteorder']['loss']-$profit['biteorder']['profit'];//计算公司盈亏

        //螺纹钢订单数据
        $order = M('Futures_order')->table('wp_futures_order as a')->join('wp_follow as b on a.user_id = b.id')->where($odr)->field('a.id,a.total_price,a.profit')->select();
				$profit['order']['title'] = '螺纹钢订单';
				foreach($order as $v){
        	$profit['order']['invest'] += $v['total_price'];
					$income = $v['profit']-$v['total_price'];
        	if($income > 0){
        		$profit['order']['profit'] += $income;
        	}else{
        		$profit['order']['loss'] += $income;
        	}
        }
        //$profit['螺纹钢订单'][5] = $profit['螺纹钢订单'][1]*0.02;
        $profit['order']['pal'] = -$profit['order']['loss']-$profit['order']['profit'];//计算公司盈亏
        //$profit['螺纹钢订单'][4] = round($profit['螺纹钢订单'][4],2);

        //充值数据
        $recharge = M('Futures_recharge')->table('wp_futures_recharge as a')->join('wp_follow as b on a.user_id = b.id')->where($rec)->field('a.money')->select();
				$gold['rec']['title'] = '充值';
				foreach($recharge as $v){
        	$gold['rec']['money'] +=	$v['money'];
        	$gold['rec']['fee'] += $v['money']*0.009;//下单手续费
        }

        //提现数据
        $withdrawals = M('futures_withdrawals')->table('wp_futures_withdrawals as a')->join('wp_follow as b on a.user_id = b.id')->where($cash)->field('a.money,a.handling_fee')->select();
				$gold['cash']['title'] = '提现';
				foreach($withdrawals as $v){
        	$gold['cash']['money'] += $v['money'];
        	$gold['cash']['fee'] += $v['handling_fee'];//提现手续费
        }

        //佣金数据
        $commission = M('Futures_commission')->table('wp_futures_commission as a')->join('wp_follow as b on a.commission_user_id = b.id')->where($cms)->field('a.commission,a.status')->select();
				$com_data['already']['title'] =  '已分配';
				$com_data['not']['title'] =  '未分配';
				foreach($commission as $v){
        	if($v['status'] == 0){
        		$com_data['not']['commission'] += $v['commission'];
        	}elseif($v['status'] == 1){
        		$com_data['already']['commission'] += $v['commission'];
        	}
        }

        //判断数据是否为空
				foreach($profit as $k => $v){
					$profit[$k]['invest'] = !empty($v['invest']) ? round($v['invest'],2) : 0;
					$profit[$k]['profit'] = !empty($v['profit']) ? round($v['profit'],2) : 0;
					$profit[$k]['loss'] = !empty($v['loss']) ? -round($v['loss'],2) : 0;
					$profit[$k]['pal'] = !empty($v['pal']) ? round($v['pal'],2) : 0;
				}
				foreach($gold as $k => $v){
					$gold[$k]['money'] = !empty($v['money']) ? round($v['money'],2) : 0;
					$gold[$k]['fee'] = !empty($v['fee']) ? round($v['fee'],2) : 0;
				}
				foreach ($com_data as $k => $v) {
					$com_data[$k]['commission'] = !empty($v['commission']) ? round($v['commission'],2) : 0;
				}


        $this->assign('sub_money',$sub_money);
        $this->assign('com_data',$com_data);
        $this->assign('profit',$profit);
        $this->assign('gold',$gold);
        $this->assign('list',$list);
		$this->display();
	}
}
